<template>
  <el-dialog
      v-model="centerDialogVisible"
      title="编辑作者"
      width="30%"
      align-center
      top="0"
      style="min-width: 600px"
  >
    <EditAuthorForm v-model="author"/>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="update">
          确认修改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import {inject} from 'vue'
import {updateAuthor} from "@/apis/author";
import {ElMessage} from "element-plus";
import EditAuthorForm from "@/components/author/EditAuthorForm.vue";

const centerDialogVisible = inject("authorEditDialog")
const author = inject('showAuthor')

const getPagedAuthors = inject('getPagedAuthors')
const update = () => {
  const it = JSON.parse(JSON.stringify(author.value))
  // 通过domainsStr获取domains数组
  let dns = author.value.domainsStr
  if (!dns) dns = ''
  const dnSet = new Set(dns.split(';')) // 去重
  it.domains = []
  dnSet.forEach(dn => {
    dn = dn.trim()
    if (dn) it.domains.push({name: dn})
  })
  updateAuthor(it).then(resp => {
    console.log(resp.data)
    if (resp.data.code === 200) {
      ElMessage.success('编辑成功')
      getPagedAuthors()
      author.value = {}
      centerDialogVisible.value = false
    } else {
      ElMessage.error(resp.data.msg)
    }
  })
}
</script>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
